<?php 

namespace admin;

/**
* 
*/
class Controller_App extends Controller_BaseController
{
    
    public $template = 'game/template';

    /*function __construct(argument)
    {
        
    }*/

    public function action_index(){
        $params = array(
            'title' => '游戏设置',
            'menu' => '',
            'module' => \Input::get('module', 'phone')
        );

        if(\Input::is_ajax()){

            $app = \Model_App::query();
            if(\Input::get('action', false)){
                if(\Input::get('action') == 'category'){
                    $app->related('propertys');
                }
                
                if(\Input::get('category_id', false)){
                    $app->where('propertys.category_id', \Input::get('category_id'));
                }else{
                    $app->where('propertys.category_id', '<>', 0);
                }
            }

            if(\Input::get('search', '') != '' && \Input::get('search')['value'] != ''){
                $value = \Input::get('search')['value'];
                $app->or_where('id', $value);
                $app->or_where('name', 'like', "%{$value}%");
                $app->or_where('tag', 'like', "%{$value}%");
                $app->or_where('categoryName', 'like', "%{$value}%");
            }

            $config = array(
                'pagination_url' => '/admin/app' . (\Input::get('action', false) ? "?action=" . \Input::get('action') : ''),
                'total_items'    => $app->count(),
                'per_page'       => \Input::get('length', 10),
                'uri_segment'    => 'start',
            );
            $pagination = \Pagination::forge('mypagination', $config);
            $params['items'] = $app->rows_offset($pagination->offset)
                                ->rows_limit($pagination->per_page)
                                ->get();
            $params['pagination'] = $pagination;

            $cats = \Model_AppPropertyCategory::query()->order_by('module')->get();

            $m = '';
            $pc = '';
            foreach ($cats as $key => $value) {
                if(strpos($value->module, 'mg') !== false){
                    $m .= "<li><a role='property' data-id='{$value->id}' href='#'>{$value->name}</a></li>";
                }else if(strpos($value->module, 'pc') !== false){
                    $pc .= "<li><a role='property' data-id='{$value->id}' href='#'>{$value->name}</a></li>";
                }
            }

            $items = array();
            foreach ($params['items'] as $key => $value) {
                $item = $value->to_array();
                $item['downloadUrl'] = "<a href='<?php echo $value->downloadUrl; ?>'>下载游戏</a>";
                $item['name'] = "<img src='{$value->iconUrl}' alt='$value->name'/><br>{$value->name}";

                if(\Input::get('action', false)){
                    $item['options'] = "<a role='del' data-id='{$value->id}' class='btn btn-sm btn-danger'> <i class='fa fa-trash-o'></i> 删除 </a>";
                }else{
                    $item['options'] = 
                    "<div class='btn-group'>
                        <button type='button' class='btn btn-sm btn-success'>手机端</button>
                        <button type='button' class='btn btn-sm btn-success dropdown-toggle' data-toggle='dropdown'>
                            <span class='caret'></span>
                            <span class='sr-only'>Toggle Dropdown</span>
                        </button>
                        <ul class='dropdown-menu' role='menu'>
                            {$m}
                        </ul>
                    </div>
                    <div class='btn-group'>
                        <button type='button' class='btn btn-sm btn-info'>PC端</button>
                        <button type='button' class='btn btn-sm btn-info dropdown-toggle' data-toggle='dropdown'>
                            <span class='caret'></span>
                            <span class='sr-only'>Toggle Dropdown</span>
                        </button>
                        <ul class='dropdown-menu' role='menu'>
                            {$pc}
                        </ul>
                    </div>";
                }
                
                array_push($items, $item);
            }
            die(json_encode(array('draw' => \Input::get('draw', 1), 'recordsTotal' => $config['total_items'], 'recordsFiltered'=> $config['total_items'], 'data' => $items)));
        }
        
        \View::set_global($params);
        $this->template->content = \View::forge("game/items");
    }

    /**
    * 游戏同步更新
    *
    */
    public function action_update(){
        $this->template->content = \View::forge("game/update");
    }

    /**
    * 游戏幻灯片管理
    *
    */
    public function action_swipe(){
        $this->template->content = "管理首页及网游页幻灯片";
    }

    /**
    * 获取所有游戏属性分类
    *
    */
    public function action_cats(){
        if(\Input::is_ajax()){
            $cats = \Model_AppPropertyCategory::query()
                        ->where('module', 'like', \Input::get('module', 'phone') == 'phone' ? 'mg-%' : 'pc-%')
                        ->get();
            $items = array();
            foreach ($cats as $key => $value) {
                array_push($items, $value->to_array());
            }
            die(json_encode(array('status' => 'succ', 'msg' => '', 'data' => $items, 'errcode' => 0)));
        }
    }

    /**
    * 设置游戏属性
    *
    */
    public function action_property(){

        if(\Input::method() == 'POST'){
            if(\Input::is_ajax()){
                $data = \Input::post();
                $app = \Model_AppProperty::query()
                    ->where('category_id', $data['category_id'])
                    ->where('app_id', $data['app_id'])->get_one();
                if($app){
                    die(json_encode(array('status' => 'err', 'msg' => '记录已存在', 'errcode' => 10)));
                }

                $app = \Model_AppProperty::forge($data);
                if($app->save()){
                    die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
                }
                die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
            }
        }
        
    }

    /**
    * 删除游戏属性
    *
    */
    public function action_del(){
        /*if( ! $id){
            if(\Input::is_ajax()){
                die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
            }
        }*/

        if(\Input::is_ajax()){
            $data = \Input::get();
            $app = \Model_AppProperty::query()
                    ->where('category_id', $data['category_id'])
                    ->where('app_id', $data['app_id'])->get_one();
            if( ! $app){
                die(json_encode(array('status' => 'err', 'msg' => '属性不存在', 'errcode' => 30)));
            }
            if($app->delete()){
                die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
            }else{
                die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
            }
            
        }
    }
}
?>